var common = require("common");
cc.Class({
    extends: cc.Component,

    properties: {
        guanqiashu:cc.Label,
        wtouxiang:cc.Sprite,
        wguanqia:cc.Label,
        btouxiang:cc.Sprite,
        bguanqia:cc.Label,
        tip:cc.Label,
        dating:cc.Button,
        nextBtn:cc.Button,
        chengyuLayout:cc.Node,
        chegnyuye:cc.Node,
        chengyuItem:cc.Prefab,
        brr:[],

        guanbiBtn:cc.Button,
        baocuoBtn:cc.Button,
        shengciBtn:cc.Button,
        fenxiangBtn:cc.Button,
        chengyu:cc.Label,
        pinyin:cc.Label,
        shiyi:cc.Label,
        chuchu:cc.Label,

        game:cc.Node,
    },
    onLoad () 
    {
        this.dating.node.on("click",this.OnDaTingBtn,this);
        this.nextBtn.node.on("click",this.OnNextBtn,this);
        this.chengyuye = cc.find("chengyuye",this.node);

        this.guanbiBtn.node.on("click",this.OnGuanBiBtn,this);
        this.baocuoBtn.node.on("click",this.OnBaoCuoBtn,this);
        this.shengciBtn.node.on("click",this.OnShengCiBtn,this);
        this.fenxiangBtn.node.on("click",this.OnFenXiangBtn,this);

        var self = this;
        cc.loader.loadRes("peizhi/chengyuciku",function(error,TestTxt) {
            self.text = TestTxt.text;
            self.node.emit("LoadFinish",{});
        });
        this.node.on("LoadFinish",function() {
            this.ChaiFen();
        },this);
    },
    OnDaTingBtn:function(event) 
    {
        common.level++;
        common.tilitubiao.active = true;
        cc.director.loadScene("Hall");
    },
    //下一关
    OnNextBtn:function(event) 
    {
        console.log("点击下一关");
        if (common.tilishu > 0) {
            common.tilishu--;
            common.level++;
            this.game.active = true;
        }
    },
    start () 
    {
        this.CreateChengYu();
    },
    //拆分词库
    ChaiFen(){
        let arr = this.text.split("\n");
        for (let i = 1; i < arr.length; i++) {
            this.brr.push(arr[i]);
        }
    },
    CreateChengYu()
    {
        var self = this;
        for (let i = 0; i < common.Idiom.length; i++) {
            let chengyu = cc.instantiate(this.chengyuItem);
            chengyu.parent = this.chengyuLayout;
            let label = chengyu.getChildByName("Label").getComponent(cc.Label);
            label.string = common.Idiom[i];
            chengyu.on("mousedown",function(event) {
                //打开成语页
                self.chengyuye.active = true;
                let cy = event.target.getChildByName("Label").getComponent(cc.Label).string;
                self.SetLabel(cy);
            },this);
        }
    },
    OnGuanBiBtn:function(event)
    {
        this.chengyuye.active = false;
    },
    OnBaoCuoBtn:function(event)
    {
        
    },
    OnShengCiBtn:function(event)
    {
        //添加到生词本
    },
    OnFenXiangBtn:function(event)
    {
        
    },
    SetLabel(cy)
    {
        for (let i = 0; i < this.brr.length; i++) {
            let str = this.brr[i].split("	");
            if (cy == str[1]) {
                this.chengyu.string = str[1];
                this.pinyin.string = str[2];
                this.shiyi.string = str[3];
                this.chuchu.string = str[4];
                return;
            }
        }
    }
    // update (dt) {},
});
